package gupao.concurrency.concurrent.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;

@Slf4j
public class CountDownLatchSample {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(5);

        for(int i = 0; i < 5; i++){
            new Thread(() -> {
                log.info("{} is running..", Thread.currentThread().getName());
                Utils.sleepIgnoreExp(2000);
                latch.countDown();
                log.info("{} finished", Thread.currentThread().getName());
            }).start();
        }

        log.info("{} waits other thread finished.", Thread.currentThread().getName());
        latch.await();
    }
}
